import { createApp } from 'vue'
import App from './App.vue'
// 引入VueRouter
import router from './route/index'
// 引入lib-flexible
// import "amfe-flexible"

// 引入pinia
import { createPinia } from 'pinia';
const pinia = createPinia()

// 引入Vant组件
import {
  Button,
  Tabbar,
  TabbarItem,
  NavBar,
  Tab,
  Tabs,
  List,
  Cell,
  CellGroup,
  ConfigProvider,
  Icon,
  Swipe,
  SwipeItem,
  Image as VanImage,
  PullRefresh,
  Tag,
  ShareSheet,
  Loading,
  Skeleton,
  Grid,
  GridItem,
  Badge,
  Search,
  Col,
  Row,
  ActionBar,
  ActionBarIcon,
  ActionBarButton,
  Popup,
  Progress,
  Divider,
  Form,
  Field,
  Toast,
  ImagePreview,
  Dialog,
  SubmitBar,
  Card,
  NoticeBar,
  RadioGroup,
  Radio,
  CouponCell,
  CouponList,
  Popover,
  Checkbox,
  CheckboxGroup,
  SwipeCell,
  Empty,
  AddressList,
  AddressEdit,
  ActionSheet,
  Area,
  Sticky,
  Lazyload
} from 'vant';

const app = createApp(App)

app.use(router)
  .use(pinia)
  .use(Button)
  .use(Tabbar)
  .use(TabbarItem)
  .use(NavBar)
  .use(Tab)
  .use(Tabs)
  .use(List)
  .use(Cell)
  .use(CellGroup)
  .use(ConfigProvider)
  .use(Icon)
  .use(Swipe)
  .use(SwipeItem)
  .use(VanImage)
  .use(PullRefresh)
  .use(Tag)
  .use(ShareSheet)
  .use(Loading)
  .use(Skeleton)
  .use(Grid)
  .use(GridItem)
  .use(Badge)
  .use(Search)
  .use(Col)
  .use(Row)
  .use(ActionBar)
  .use(ActionBarIcon)
  .use(ActionBarButton)
  .use(Popup)
  .use(Progress)
  .use(Divider)
  .use(Form)
  .use(Field)
  .use(Toast)
  .use(ImagePreview)
  .use(Dialog)
  .use(SubmitBar)
  .use(Card)
  .use(NoticeBar)
  .use(Radio)
  .use(RadioGroup)
  .use(CouponCell)
  .use(CouponList)
  .use(Popover)
  .use(Checkbox)
  .use(CheckboxGroup)
  .use(SwipeCell)
  .use(Empty)
  .use(AddressList)
  .use(AddressEdit)
  .use(ActionSheet)
  .use(Area)
  .use(Sticky)
  .use(Lazyload)
  .mount('#app')

// 路由守卫
// 允许未登录时预览的页面
const releasePath = [
  '/home/rec', '/profile',
  '/gamestore/rec', '/gamestore/rank',
  '/gamedetail', '/login',
  '/community/rec', '/community/hotlist',
]
router.beforeEach((to, from, next) => {
  if (releasePath.includes(to.path)) {
    next()
  } else {
    const tokenStr = window.sessionStorage.getItem('token')
    if (tokenStr) {
      console.log('已经登陆');
      next()
    } else {
      Dialog.confirm({
        title: '请先登录哦~',
        confirmButtonText: '去登录',
        confirmButtonColor: '#000000',
        cancelButtonColor: '#969799'
      }).then(() => {
        return router.push('/profile')
      }).catch(() => {
        return
      });
    }
  }
})